﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;

namespace PresentationLayer.View.StoreClerk
{
    public partial class AdjustmentSubmit : System.Web.UI.Page
    {

        private static List<AdjustmentCatalogue> AdjCatlist;
        protected void Page_Load(object sender, EventArgs e)
        {
           
              if (!IsPostBack)
              {
              
                   //string adjDate = DateTime.Now.ToString("MM/dd/yyyy");
                   string adjDate = DateTime.Now.ToString("dd/MM/yyyy");
                   lblTodayDate.Text = adjDate;
                
                   AdjCatlist = new List<AdjustmentCatalogue>();
                   if (Session["AdjCatlist"] != null) AdjCatlist = (List<AdjustmentCatalogue>)Session["AdjCatlist"];
                   
                
                   if (AdjCatlist==null)
                   {
                      gvCatalist.DataSource = null;
                      gvCatalist.DataBind();
                    
                   }
                   else
                   {
                      gvCatalist.DataSource = AdjCatlist;
                      gvCatalist.DataBind();
                      btnSubmit.Visible = true;
                   }
                   
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {       string SelCat="";
                List<Catalogue> allCatlist = new List<Catalogue>();
                if(Session["Allcatlist"]!=null) allCatlist = (List<Catalogue>)Session["Allcatlist"];
            

                //for generate Adjustment Voucher
                if (Session["SelCat"]!=null) SelCat = Session["SelCat"].ToString();
                string submit_date = lblTodayDate.Text;

            
               /*If AdjustmentItem List has no Items--> will not process*/
               /*********************************************************/
               if (AdjCatlist.Count != 0 && allCatlist.Count!=0 && SelCat != "" && submit_date != "")
               {
                    long Requser = 0;
                    Employee Requserobj = new Employee();
                    if (Session["CurrentUser"] != null)
                    {
                        Requserobj = (Employee)Session["CurrentUser"];
                        Requser = Convert.ToInt64(Requserobj.EmpId);
                    }


                    int myresult = MakeStationeryAdjController.getMakeStationeryAdjController().Process(AdjCatlist, allCatlist, SelCat, submit_date, Requser);
                    lblmsg.Visible = true;
                    lblmsg.Text = "Adjusted Items are successfully submitted for approval.";
                    gvCatalist.Visible = false;
                    btnSubmit.Visible = false;
                    Session.Remove("Allcatlist");
                    Session.Remove("SelCat");
                    Session.Remove("AdjCatlist");
             
                }
               else
               {
                   lblmsg.Visible = true;
                   lblmsg.Text = "You haven't added any stationery Items for adjustment.";
                   btnSubmit.Visible = false;
               }
        }

        protected void gvCatalist_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
                 /* Gridview Rebinding for new Page */
                /***********************************/
                   gvCatalist.PageIndex = e.NewPageIndex;
           
                   if (AdjCatlist==null)
                  {
                     gvCatalist.DataSource = null;
                     gvCatalist.DataBind();

                  }
                  else
                  {
                     gvCatalist.DataSource = AdjCatlist;
                     gvCatalist.DataBind();
                     btnSubmit.Visible = true;
                   }
        }

      
    }
}